//
//  ContainerViewController.m
//  ContainerDemo
//
//  Created by qianfeng on 15/3/3.
//  Copyright (c) 2015年 WeiZhenLiu. All rights reserved.
//

#import "ContainerViewController.h"
#import "Topbar.h"
#import "ViewWillShow.h"
#import "AlbumViewController.h"
@interface ContainerViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) Topbar *topbar;
@property (nonatomic, assign) NSInteger currentPage;
@end

@implementation ContainerViewController

- (id)initWithViewControllers:(NSArray *)viewControllers {
    if (self = [super init]) {
        _viewControllers = viewControllers;
        
    }
    return self;
}
-(void)myPush:(NSArray *)arr{
    for (BaseViewController * vc in arr) {
        __weak typeof(self)weakSelf=self;
        NSLog(@"---%@",[vc class]);
        
        [vc addJumpEvent:^(id obj) {
            AlbumViewController * albumView=[[AlbumViewController alloc] init];
            MediaListModel *model=obj;
            albumView.model=model;
            
            [weakSelf.navigationController pushViewController:albumView animated:YES];
        }];

    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView * navi=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kSreenSize.width, 64)];
    navi.userInteractionEnabled=YES;
    navi.image=[UIImage imageNamed: @"navicagationBar"];
    [self.view addSubview:navi];
    UIButton * button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(kSreenSize.width-40,20+5, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed: @"bf_search_white"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    self.navigationController.navigationBar.hidden=YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
}

- (Topbar *)topbar {
    static NSInteger i = 0;
    i++;
    NSLog(@"i:----->%ld",i);
       if (!_topbar) {
        _topbar = [[Topbar alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.frame), kTopbarHeight)];

        
        __block ContainerViewController *_self = self;
        _topbar.blockHandler = ^(NSInteger currentPage) {
            [_self setCurrentPage:currentPage];
        };
        [self.view addSubview:_topbar];
    }
    return _topbar;
}


- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,64,kSreenSize.width,kSreenSize.height-64-55)];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.delegate                       = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator   = NO;
        _scrollView.bounces                        = NO;
        _scrollView.pagingEnabled                  = YES;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

// overwrite setter of property: viewControllers
- (void)setViewControllers:(NSArray *)viewControllers {
    [self myPush:viewControllers];
    self.automaticallyAdjustsScrollViewInsets=NO;

    _viewControllers = [NSArray arrayWithArray:viewControllers];
    CGFloat x = 0.0;
    for (UIViewController *viewController in _viewControllers) {
        [viewController willMoveToParentViewController:self];
        viewController.view.frame = CGRectMake(x, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        [self.scrollView addSubview:viewController.view];
        [viewController didMoveToParentViewController:self];
        
        x += CGRectGetWidth(self.scrollView.frame);
        _scrollView.contentSize   = CGSizeMake(x, _scrollView.frame.size.width);
    }
    
    self.topbar.titles = [_viewControllers valueForKey:@"title"];
}

- (void)setCurrentPage:(NSInteger)currentPage {
    _currentPage = currentPage;
    [self.scrollView setContentOffset:CGPointMake(_currentPage*_scrollView.frame.size.width, 0) animated:NO]; //
}

- (void)layoutSubViews
{
    CGFloat x = 0.0;
    for (UIViewController *viewController in self.viewControllers) {
        viewController.view.frame = CGRectMake(x, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
        x += CGRectGetWidth(self.scrollView.frame);
           }
    self.scrollView.contentSize   = CGSizeMake(x, _scrollView.frame.size.width);
    //self.scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger currentPage = _scrollView.contentOffset.x / _scrollView.frame.size.width;
    _topbar.currentPage   = currentPage;
    _currentPage = currentPage;
    [self callbackSubViewControllerWillShow];
}

// call back if scroll to special view controller
- (void)callbackSubViewControllerWillShow {
    UIViewController<ViewWillShow> *controller = [self.viewControllers objectAtIndex:self.currentPage];
    
    
    if ([controller conformsToProtocol:@protocol(ViewWillShow)] && [controller respondsToSelector:@selector(viewWillShow)]) {
       
        [controller viewWillShow];
    }
}

@end



